package com.ybwh.springboot2;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.ybwh.springboot2.common.BusinessException;
import com.ybwh.springboot2.excel.CustomRepayScheduleImport;
import com.ybwh.springboot2.excel.FactorCustomRepayPlanParser;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author fanbeibei
 * @version 1.0.0
 * @date 2023/9/12 18:00
 */
public class ReadExcel {


    public void test() throws FileNotFoundException {

        // 指定文件
        ExcelReader excelReader = EasyExcel.read(new FileInputStream(new File("D:/xx.xlsx"))).build();

        // 指定读取的方式
        FactorCustomRepayPlanParser parser = new FactorCustomRepayPlanParser();
        ReadSheet readSheetAsset =EasyExcel.readSheet(0)
                .head(CustomRepayScheduleImport.class)
                .headRowNumber(4)
                .registerReadListener(parser)
                .build();

        // 正式开始读取
        excelReader.read(readSheetAsset);

        List<CustomRepayScheduleImport> excelData = parser.getExcelDatas();
        if(CollectionUtils.isEmpty(excelData)){
            throw new BusinessException("导入的文件无数据");
        }
    }




}
